package pers.lonestar.framework.utils.support.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import org.apache.poi.util.TempFile;

import pers.lonestar.framework.utils.support.exception.ExcelAnalysisException;
import pers.lonestar.framework.utils.support.exception.ExcelUtilsException;

public class FileUtil {
    public static final String EX_CACHE = "excache";
    private static final int WRITE_BUFF_SIZE = 8192;

    private static final String TEMP_FILE_PREFIX =
            System.getProperty(TempFile.JAVA_IO_TMPDIR) + File.separator + UUID.randomUUID() + File.separator;

    private static final String CACHE_PATH = TEMP_FILE_PREFIX + EX_CACHE + File.separator;

    static {
        // Create a temporary directory in advance
        File tempFile = new File(TEMP_FILE_PREFIX);
        createDirectory(tempFile);
        tempFile.deleteOnExit();
        // Initialize the cache directory
        File cacheFile = new File(CACHE_PATH);
        createDirectory(cacheFile);
        cacheFile.deleteOnExit();
    }

    private FileUtil() {
    }

    public static void writeToFile(File file, InputStream inputStream) {
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            int bytesRead;
            byte[] buffer = new byte[WRITE_BUFF_SIZE];
            while ((bytesRead = inputStream.read(buffer, 0, WRITE_BUFF_SIZE)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            throw new ExcelAnalysisException("Can not create temporary file!", e);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new ExcelAnalysisException("Can not close 'outputStream'!", e);
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new ExcelAnalysisException("Can not close 'inputStream'", e);
                }
            }
        }
    }

    public static File createCacheTmpFile() {
        return createDirectory(new File(CACHE_PATH + UUID.randomUUID()));
    }

    private static File createDirectory(File directory) {
        if (!directory.exists() && !directory.mkdirs()) {
            throw new ExcelUtilsException("Cannot create directory:" + directory.getAbsolutePath());
        }
        return directory;
    }

    public static void delete(File file) {
        if (file.isFile()) {
            file.delete();
            return;
        }
        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                file.delete();
                return;
            }
            for (int i = 0; i < childFiles.length; i++) {
                delete(childFiles[i]);
            }
            file.delete();
        }
    }

}
